package com.bigzero.workflow.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigzero.workflow.common.exception.WorkflowException;
import com.bigzero.workflow.modules.process.entity.DeployNodeEntity;
import com.bigzero.workflow.modules.system.dao.RoleDao;
import com.bigzero.workflow.modules.system.dao.RoleMenuDao;
import com.bigzero.workflow.modules.system.dao.UserRoleDao;
import com.bigzero.workflow.modules.system.dto.RoleDto;
import com.bigzero.workflow.modules.system.entity.RoleEntity;
import com.bigzero.workflow.modules.system.entity.RoleMenuEntity;
import com.bigzero.workflow.modules.system.entity.UserRoleEntity;
import com.bigzero.workflow.modules.system.query.RoleQuery;
import com.bigzero.workflow.modules.system.service.RoleService;
import com.bigzero.workflow.modules.system.vo.RoleVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 角色实现类
 * @Author: fanxinxiong
 * @Date: 2025-02-11 14:48:00
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, RoleEntity> implements RoleService{

    @Autowired
    private RoleDao roleDao;
    @Autowired
    private UserRoleDao userRoleDao;
    @Resource
    private RoleMenuDao roleMenuDao;

    @Override
    public void deleteRole(String roleId) {
        if(userRoleDao.selectCount(Wrappers.<UserRoleEntity>lambdaQuery().eq(UserRoleEntity :: getRoleId, roleId)) >= 1) {
            throw new WorkflowException("角色已绑定用户!", 400);
        }
        roleDao.deleteById(roleId);
    }

    @Override
    public void deleteBatch(List<String> roleIds) {
        if (userRoleDao.selectCount(Wrappers.<UserRoleEntity>lambdaQuery()
                .in(UserRoleEntity :: getRoleId, roleIds)) > 0) {
            throw new WorkflowException("角色已绑定用户!", 400);
        }
        roleDao.delete(Wrappers.lambdaUpdate(RoleEntity.class)
                .in(RoleEntity :: getId, roleIds));
    }

    @Override
    public List<String> getRoleCodeByUserCode(String userCode) {
        return roleDao.getRoleCodeByUserCode(userCode);
    }

    @Override
    public List<String> getUserCodeByRoleCode(String roleCode) {
        return roleDao.getUserCodeByRoleCode(roleCode);
    }

    /**
     * 角色分页列表
     *
     * @param roleQuery 参数
     * @return 列表
     */
    @Override
    public List<RoleVo> queryPage(RoleQuery roleQuery) {
        return baseMapper.queryPage(roleQuery);
    }

    /**
     * 获取所有实体集合
     * @return List<RoleEntity>
     */
    public List<RoleEntity> getAllEntityList(){
        LambdaQueryWrapper lqw = new LambdaQueryWrapper<DeployNodeEntity>();
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据id获取vo对象
     * @param id 主键id
     * @return UserVo
     */
    public RoleVo getVoById(String id){
        RoleEntity entity = baseMapper.selectById(id);
        if(Objects.isNull(entity)){
            return null;
        }
        RoleVo vo = new RoleVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    /**
     * 新增或者修改
     * @param roleDto dto实体
     * @return int
     */
    public int insertOrUpdate(RoleDto roleDto){
        Date date = new Date();

        LambdaQueryWrapper<RoleEntity> codeLqw = new LambdaQueryWrapper<RoleEntity>();
        codeLqw.eq(RoleEntity :: getCode, roleDto.getCode());
        LambdaQueryWrapper<RoleEntity> nameLqw = new LambdaQueryWrapper<RoleEntity>();
        nameLqw.eq(RoleEntity :: getName, roleDto.getName());
        //修改
        if(!org.apache.commons.lang3.StringUtils.isBlank(roleDto.getId())){
            RoleEntity roleEntity = roleDao.selectById(roleDto.getId());
            if(roleEntity == null) {
                throw new WorkflowException("角色不存在!");
            }
            codeLqw.ne(RoleEntity :: getId, roleDto.getId());
            nameLqw.ne(RoleEntity :: getId, roleDto.getId());
        }
        long codeCount = roleDao.selectCount(codeLqw);
        if(codeCount > 1){
            throw new WorkflowException("角色编码[" + roleDto.getCode() +"]已存在!");
        }
        long nameCount = roleDao.selectCount(nameLqw);
        if(nameCount > 1) {
            throw new WorkflowException("角色名称[" + roleDto.getName() +"]已存在!");
        }

        RoleEntity roleEntity = new RoleEntity();
        roleEntity.setCode(roleDto.getCode());
        roleEntity.setName(roleDto.getName());
        roleEntity.setUpdateTime(date);
        if(org.apache.commons.lang3.StringUtils.isBlank(roleDto.getId())){
            roleEntity.setCreateTime(date);
            return roleDao.insert(roleEntity);
        }else{
            roleEntity.setId(roleDto.getId());
            return roleDao.updateById(roleEntity);
        }
    }

    /**
     * 根据id删除用户
     * @param id 主键id
     * @return int
     */
    public int deleteById(String id){
        RoleEntity role = baseMapper.selectById(id);
        if(role == null){
            throw new WorkflowException("未知数据");
        }
        LambdaQueryWrapper<UserRoleEntity> ureLqw = new LambdaQueryWrapper<UserRoleEntity>();
        ureLqw.eq(UserRoleEntity :: getRoleId, id);
        userRoleDao.delete(ureLqw);

        LambdaQueryWrapper<RoleMenuEntity> rmeLqw = new LambdaQueryWrapper<RoleMenuEntity>();
        rmeLqw.eq(RoleMenuEntity :: getRoleId, id);
        roleMenuDao.delete(rmeLqw);

        return baseMapper.deleteById(id);
    }
}
